#ifndef __CONFG_INI_H_
#define __CONFG_INI_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <ctype.h>

#define dbg(fmt, ...) do {printf("\033[0m[DEBUG %s:%d:%s]" fmt, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);} while(0)
#define info(fmt, ...) do {printf("\033[0;32m[INFO  %s:%d:%s]\033[0m" fmt, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);} while(0)
#define warn(fmt, ...) do {printf("\033[0;33m[WARN  %s:%d:%s]\033[0m" fmt, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);} while(0)
#define err(fmt, ...) do {printf("\033[0;31m[ERROR %s:%d:%s]" fmt, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); printf("\033[0m");} while(0)

#define CONFIG_INI_FILE "/etc/conf/config.ini"

typedef struct {
    char* key;
    char* value;
} ini_item_t;

typedef struct {
    char* name;
    ini_item_t* items;
    int num_items;
} ini_section_t;

typedef struct {
    ini_section_t* sections;
    int num_sections;
    pthread_mutex_t mutex;
} ini_config_t;

extern ini_config_t *gConfig;
void ini_config_init(ini_config_t* config);
ini_config_t* ini_config_load(ini_config_t* config, const char* filename);
int find_item_index(ini_config_t* config, const char* section_name, const char* key, int *section_index);
int ini_config_add_or_update(ini_config_t* config, const char* section_name, const char* key, const char* value);
int ini_config_get(ini_config_t* config, const char* section_name, const char* key, char* value);
int ini_config_save(const char* filename, ini_config_t* config);
int ini_remove_item(ini_config_t* config, const char* section_name, const char* key);
void free_ini_config(ini_config_t* config) ;
#endif